/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ourMessage;

import java.io.*;

/**
 *
 * @author olga
 * @author adrian
 */
public class Message implements Serializable {

    private static final long serialVersionUID = 31400L;
    protected String login;

    public Message(String login) { //login source
        this.login = login;
    }

    @Override
    public String toString() {
        return "Message from " + login;
    }

    public String getlogin() {
        return login;
    }

    public void setlogin(String login) {
        this.login = login;
    }

    public static byte[] toArray(Message msg) throws IOException {
        ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
        ObjectOutputStream objectOut = new ObjectOutputStream(byteOut);
        objectOut.writeObject(msg);
        objectOut.close();

        byte[] flux = byteOut.toByteArray();
        byteOut.close();
        /**
         * // retourne un tableau d'octet
         */
        return flux;
    }

    /**
     *
     * @param array
     * @return the message
     * @throws IOException
     * @throws ClassNotFoundException
     */
    public static Message fromArray(byte[] array) throws IOException, ClassNotFoundException {
        ByteArrayInputStream byteIn = new ByteArrayInputStream(array);
        ObjectInputStream objectIn = new ObjectInputStream(byteIn);

        Message msg;
        msg = (Message) objectIn.readObject();

        return msg;
    }

}
